/********************************************************************************************/
/* Fullhan Technology (Shanghai) Co., Ltd.                                                  */
/* Fullhan Proprietary and Confidential                                                     */
/* Copyright (c) 2017 Fullhan Technology Group Limited and its affiliated companies         */
/********************************************************************************************/
#include "drv_cesa_if.h"
#include "hw_cesa_if.h"
#include <linux/slab.h>
#include <linux/uaccess.h>
#include <linux/dma-mapping.h>


void drv_cesa_aux_init(FH_CESA_AUX_S *p_aux)
{
	sema_init(&p_aux->sem_ades, 1);
	sema_init(&p_aux->sem_sha, 1);
	sema_init(&p_aux->sem_rsa, 1);

	p_aux->keep_ades = 0;
}

void drv_cesa_aux_deinit(FH_CESA_AUX_S *p_aux)
{
	p_aux->keep_ades = 0;
}

void drv_cesa_ades_lock(uint32_t session)
{
	FH_CESA_AUX_S *p_aux = (FH_CESA_AUX_S *)((HW_CESA_ADES_CTRL_S *)session)->priv;

	if (p_aux) {
		if (down_interruptible(&p_aux->sem_ades))
			printk("ades lock failed!\n");
	}
}

void drv_cesa_ades_unlock(uint32_t session)
{
	FH_CESA_AUX_S *p_aux = (FH_CESA_AUX_S *)((HW_CESA_ADES_CTRL_S *)session)->priv;

	if (p_aux)
		up(&p_aux->sem_ades);
}



bool drv_cesa_check_keep(uint32_t session, uint32_t same_diff_both)
{
	FH_CESA_AUX_S *p_aux = (FH_CESA_AUX_S *)((HW_CESA_ADES_CTRL_S *)session)->priv;

	if (p_aux) {
		if (same_diff_both == 0) {
			if (p_aux->keep_ades == session) {
				p_aux->keep_ades = 0;
				return true;
			}
		}

		if (same_diff_both == 1) {
			if (p_aux->keep_ades != session) {
				p_aux->keep_ades = session;
				return true;
			}
		}

		if (same_diff_both == 2) {
			if (p_aux->keep_ades == session) {
				p_aux->keep_ades = 0;
				return true;
			}
			else {
				p_aux->keep_ades = session;
				return true;
			}
		}

		return false;
	} else {
		return true;
	}
}

void drv_cipher_data_print(int8_t *string, uint8_t *data, uint32_t length, uint8_t align)
{
	uint32_t i;

	if (string != NULL)
	{
		printk("%s \n", string);
	}

	if (data != NULL)
	{
		for(i = 0; i < length; i++)
		{
			printk("%02x ", data[i]);
			if(((i+1)%align) == 0)//(0 == ((i+1) & (align-1)))
			{
				printk("\n");
			}
		}
		printk("\n");
	}
}

